/*
 * @Descripttion: 拓展context
 * @version: 
 * @Author: turingchang
 * @Date: 2020-08-23 09:17:28
 * @LastEditors: turingchang
 * @LastEditTime: 2020-08-28 11:29:12
 */

'use strict';
const CustomLogger = require('./../common/log/custom_logger');

const user = Symbol('Context#user');
const token = Symbol('Context#token');
const requestLogger = Symbol('Context#requestLogger');
const start_time = Symbol('Context#start_time');
const end_time = Symbol('Context#end_time');
const interval = Symbol('Context#interval');

module.exports = {
    //每次请求都分配一个请求id
    get request_id(){
        return this.app.utils.generator.uid(false);
    } ,
    get request_url(){
        var url =`${this.method.toLowerCase()}:${this.path}`;
        //console.log(this.request.header.host+this.request.url);
        return url;
    },
    get token(){
        var t =this.header.token;
        this.app.token = t;
        return t;
    },
    set token(t){
        this[token] =t;
        this.app.token =t;
    },
    get user(){
        return this[user];
    },
    set user(u){
        this[user]=u;
        this.app.user =u;
    },    
    get start_time() {
        if (!this[start_time]) {
            this[start_time] = start_time;
        }
        return this[start_time];
    },
    set start_time(t) {
        this[start_time]=t;
    },
    get end_time() {
        if (!this[end_time]) {
            this[end_time] = end_time;
        }
        return this[end_time];
    },
    set end_time(t) {
        this[end_time]=t;
    },
    get interval() {
        if (!this[interval]) {
            this[interval] = interval;
        }
        return this[interval];
    },
    set interval(t) {
        this[interval]=t;
    },
    get requestLogger() {
        if (!this[requestLogger]) {
            this[requestLogger] = CustomLogger(this,this.app.config.userLogger.requestLogger);
        }
        return this[requestLogger];
    },
    //定义输出功能，向浏览器输出数据
    outSuccess(msg = '', data = {},response_code_type = null) {
        if (this.app.utils.validator.isNull(data)) {
            data = {};
        }
        if (this.app.utils.validator.isNull(msg)) {
            msg = '';
        }
        this.set('Content-Type', 'application/json;charset=utf-8');
        this.body = {
            request_id: this.request_id,
            request_url: this.request_url,
            code: this.app.enums.response_code_enum.success.code,
            info: this.app.enums.response_code_enum.success.info,
            msg: msg,
            data: data
        };
    },
    outError(msg = '', data = {}, response_code_type = null) {
        if (this.app.utils.validator.isNull(response_code_type)) {
            response_code_type = this.app.enums.response_code_enum.server_error;
        }
        if (this.app.utils.validator.isNull(data)) {
            data = {};
        }
        if (this.app.utils.validator.isNull(msg)) {
            msg = response_code_type.msg;
        }
        //this.status =500;
        this.body = {
            request_id: this.request_id,
            request_url: this.request_url,
            code: response_code_type.code,
            info: response_code_type.info,
            msg: msg,
            data: data
        };
    },
    outNotFound(msg) {
        if(this.app.utils.validator.isNull(msg)){
            msg ='';
        }
        msg = msg || this.app.enum.not_found.msg;
        this.status =404;
        this.body = {
            request_id: this.request_id,
            request_url: this.request_url,
            code: this.app.enum.not_found.code,
            info: this.app.enum.not_found.info,
            msg: msg,
            data: data
        };
    }


};